import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/routes/tab_chats/controller/xindong_chats_tab_ctr.dart';
import 'package:xindong/routes/tab_chats/controller/xindong_liaotian_vm.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_tab_indicator.dart';
import 'package:xindong/widgets/container/xindong_home_container.dart';

class XinDongChatsYeMian extends StatefulWidget {
  const XinDongChatsYeMian({super.key});

  @override
  State<XinDongChatsYeMian> createState() => _XinDongChatsYeMianState();
}

class _XinDongChatsYeMianState extends State<XinDongChatsYeMian>
    with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
  final _tabController = Get.put(XinDongChatsTabController());

  @override
  void initState() {
    super.initState();

    // Tab控制器
    _tabController.initWithVsync(this);
  }

  // 避免状态重置
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return XinDongHomeBackgroundView(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: XinDongAppBar(
          "",
          leading: const SizedBox(),
          leadingWidth: 0.0,
          elevation: 0.0,
          backgroundColor: Colors.transparent,
          titleView: Row(
            children: [
              Expanded(
                child: TabBar(
                  isScrollable: true,
                  indicator: const XinDongTabIndicator(color: DZColors.mainColor),
                  indicatorSize: TabBarIndicatorSize.label,
                  labelPadding: const EdgeInsets.only(left: 16, right: 5),
                  labelColor: DZColors.titleColor,
                  unselectedLabelColor: DZColors.placeholderColor,
                  labelStyle: const TextStyle(fontSize: kTitleFontSize, fontWeight: FontWeight.w500),
                  unselectedLabelStyle: const TextStyle(fontSize: 16),
                  tabs: _tabController.tabBar(),
                  controller: _tabController.tabController,
                ),
              ),
              _popupMenuButton(context),
            ],
          ),
        ),
        body: Stack(
          children: [
            TabBarView(
              controller: _tabController.tabController,
              children: _tabController.tabBarViews(),
            ),
            // // 提示框
            // GetBuilder<XinDongPermissionController>(builder: (permission) {
            //   return XinDongPromptDialogBox(
            //     show: permission.enableLocationServices == false || permission.enablePositioningSystem == false,
            //     title: "打开位置定位，匹配附近用户",
            //     content: "找附近的人去聊天吧",
            //     buttonName: "去开启",
            //     onTapSetting: () async {
            //       await XinDongGaoDeMap.to.startLocation();
            //       permission.setup();
            //     },
            //   );
            // }),
          ],
        ),
      ),
    );
  }

  Widget _popupMenuButton(BuildContext context) {
    return GetBuilder<XinDongChatsTabController>(
      builder: (controller) {
        if (controller.currentIndex != 0) {
          return const SizedBox();
        }
        return PopupMenuButton(
          color: DZColors.backgroundColor,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
          offset: const Offset(36, kAppBarHeight + 10),
          icon: const Icon(Icons.cleaning_services_rounded, color: DZColors.titleColor),
          iconSize: 24,
          itemBuilder: (BuildContext context) {
            return [
              const PopupMenuItem(
                padding: EdgeInsets.only(left: 24),
                value: "全部已读",
                child: Text("全部已读", style: TextStyle(color: DZColors.titleColor, fontSize: 16)),
              ),
              const PopupMenuItem(
                padding: EdgeInsets.only(left: 24),
                value: "清空列表",
                child: Text("清空列表", style: TextStyle(color: DZColors.titleColor, fontSize: 16)),
              ),
            ];
          },
          onSelected: (text) {
            switch (text) {
              case "全部已读":
                XinDongLiaoTianVM.to.markAllUnread();
                break;
              case "清空列表":
                XinDongLiaoTianVM.to.removeAllConversion();
                break;
            }
          },
        );
      },
    );
  }
}
